/**
 * Author : Jorge Mejuto Create : 19/05/2009
 */

package net.eyra.negocio.dao;

import java.util.List;

import net.eyra.negocio.entidades.gestion.propietarios.fincas.Finca;

import org.hibernate.HibernateException;
import org.hibernate.Session;

public class FincaDao {

    public Finca getFinca(int id_finca) throws EyraDaoException {
        Session session = null;
        Finca finca = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            finca = (Finca) session.get(Finca.class, id_finca);
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return finca;
    }

    public List<Finca> getNumerosFincas() throws EyraDaoException {
        Session session = null;
        List<Finca> _lista = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            _lista = session.createSQLQuery("SELECT id_finca, numero_finca " + "FROM fincas " + "ORDER BY numero_finca").list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return _lista;
    }

    public void setFinca(Finca finca) throws EyraDaoException {
        Session session = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            session.saveOrUpdate(finca);
            session.flush();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
    }

}
